import { Column, PrimaryGeneratedColumn, OneToMany } from "typeorm";
import { EntityModel } from "@midwayjs/orm";
import { Users } from "./Users.entity";
import { UserChild } from "./UserChild.entity";

@EntityModel("child", { schema: "heart_neighbor" })
export class Child {
  @PrimaryGeneratedColumn({ type: "int", name: "id", comment: "Primary Key" })
  id!: number;

  @Column("varchar", { name: "name", comment: "姓名", length: 255 })
  name!: string;

  @Column("varchar", { name: "phone", comment: "电话", length: 20 })
  phone!: string;

  @Column("varchar", {
    name: "home_address",
    nullable: true,
    comment: "家庭地址",
    length: 255,
  })
  homeAddress!: string | null;

  @Column("date", { name: "birthday", nullable: true, comment: "生日" })
  birthday!: string | null;

  @Column("varchar", {
    name: "head_img",
    nullable: true,
    comment: "头像",
    length: 255,
  })
  headImg!: string | null;

  @Column("varchar", {
    name: "guardian",
    nullable: true,
    comment: "目前监护人",
    length: 255,
  })
  guardian!: string | null;

  @Column("tinyint", {
    name: "gender",
    nullable: true,
    comment: "性别，1男2女",
    width: 1,
    default: () => "'1'",
  })
  gender!: boolean | null;

  @Column("tinyint", {
    name: "is_disabled",
    comment: "是否禁用，0=启用，1=禁用",
    width: 1,
    default: () => "'0'",
  })
  isDisabled!: boolean;

  @Column("timestamp", {
    name: "created_at",
    comment: "创建时间",
    default: () => "CURRENT_TIMESTAMP",
  })
  createdAt!: Date;

  // 一对多关系：一个儿童可以有多个关联用户（监护人）- 保留原有关系兼容性
  @OneToMany(() => Users, (user) => user.child, { createForeignKeyConstraints: false })
  users!: Users[];

  // 一对多关系：一个儿童可以有多个用户关联记录
  @OneToMany(() => UserChild, (userChild) => userChild.child, { createForeignKeyConstraints: false })
  userChildren!: UserChild[];
}
